package com.zhuchaooasystem.ui.setting;

import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hwangjr.rxbus.RxBus;
import com.zhuchaooasystem.R;
import com.zhuchaooasystem.base.App;
import com.zhuchaooasystem.base.BaseActivity;
import com.zhuchaooasystem.base.BaseEvent;
import com.zhuchaooasystem.entity.UserInfo;
import com.zhuchaooasystem.presenter.setting.SettingContract;
import com.zhuchaooasystem.presenter.setting.SettingPresenter;
import com.zhuchaooasystem.ui.login.LoginActivity;
import com.zhuchaooasystem.utils.DataCleanManager;
import com.zhuchaooasystem.widget.StateButton;

import butterknife.BindView;
import butterknife.OnClick;
import cn.jpush.android.api.JPushInterface;

public class SettingActivity extends BaseActivity<SettingPresenter>
        implements SettingContract.View {


    @BindView(R.id.actionbar_iv_back)
    ImageView actionbarIvBack;
    @BindView(R.id.actionbar_txt_title)
    TextView actionbarTxtTitle;
    @BindView(R.id.act_setting_cache)
    TextView actSettingCache;
    @BindView(R.id.act_setting_cache_layout)
    RelativeLayout actSettingCacheLayout;
    @BindView(R.id.act_setting_login_state)
    StateButton stateButton_login_state;
    @BindView(R.id.act_setting_change_password_layout)
    RelativeLayout actSettingChangePasswordLayout;


    @Override
    protected int getLayout() {
        return R.layout.activity_setting;
    }

    @Override
    protected SettingPresenter getPresenter() {
        return new SettingPresenter(mContext, this);
    }

    @Override
    protected void initVariables() {

    }

    @Override
    protected void initView() {
        actionbarTxtTitle.setText("设置");
        actionbarIvBack.setVisibility(View.VISIBLE);

        try {
            actSettingCache.setText(DataCleanManager.getTotalCacheSize(App.getAppContext()));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void initDataAndLoadData() {

    }


    @OnClick({R.id.actionbar_iv_back, R.id.act_setting_cache_layout,
            R.id.act_setting_change_password_layout, R.id.act_setting_login_state})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.actionbar_iv_back:
                SettingActivity.this.finish();
                break;
            //清空缓存
            case R.id.act_setting_cache_layout:
                try {
                    DataCleanManager.clearAllCache(App.getAppContext());
                    actSettingCache.setText(DataCleanManager.getTotalCacheSize(App.getAppContext()));
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            //退出登录
            case R.id.act_setting_login_state:
                mPresenter.showDialog();
                break;
            //更改密码
            case R.id.act_setting_change_password_layout:
                Intent intent = new Intent(SettingActivity.this, ChangePasswordActivity.class);
                startActivity(intent);
                break;

        }
    }

    @Override
    public void logout() {
        UserInfo.clearLoginInfo();
        RxBus.get().post("logout", new BaseEvent<>());
        JPushInterface.stopPush(getApplicationContext());
        Intent intent_login = new Intent(SettingActivity.this, LoginActivity.class);
        //清空所有的activity
        intent_login.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(intent_login);

        SettingActivity.this.finish();
    }

}
